package com.online.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.online.pojo.User;
import com.online.pojo.UserRole;
import com.online.user.mapper.UserRoleMapper;
import com.online.user.service.UserService;
import com.online.user.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.List;

/**
* @author 瑾
* @description 针对表【on_user】的数据库操作Service实现
* @createDate 2025-01-26 18:53:38
*/
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper iUserMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<User> selectAll() {
        LambdaQueryWrapper <User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getIsDelete,0);
        return iUserMapper.selectList(lqw);
    }


    @Override
    public List<User> selectList(User iUser) {
        LambdaQueryWrapper <User> lqw = this.lqw(iUser);
        return iUserMapper.selectList(lqw);
    }

    @Override
    public Integer insert(User iUser) {
        Integer userid = iUser.getId();
        userRoleMapper.insert(new UserRole(userid,1));
        return iUserMapper.insert(iUser);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                User iUser = new User();
                iUser.setId(Integer.valueOf(id));
                iUser.setIsDelete(1);
                iUserMapper.updateById(iUser);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(User iUser) {
        return iUserMapper.updateById(iUser);
    }

    @Override
    public Page<User> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<User>)this.selectAll();
    }

    @Override
    public Page<User> pageList(User iUser, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<User>)this.selectList(iUser);
    }

    public LambdaQueryWrapper lqw(User iUser){
        LambdaQueryWrapper <User> lqw = new LambdaQueryWrapper<>();
        if (iUser.getId() != null){
            lqw.eq(User::getId,iUser.getId());
        }
        if (iUser.getUsername() != null){
            lqw.eq(User::getUsername,iUser.getUsername());
        }
        if (iUser.getPassword() != null){
            lqw.eq(User::getPassword,iUser.getPassword());
        }
        if (iUser.getAvatar() != null){
            lqw.eq(User::getAvatar,iUser.getAvatar());
        }
        if (iUser.getPhoneNum() != null){
            lqw.eq(User::getPhoneNum,iUser.getPhoneNum());
        }
        if (iUser.getEmail() != null){
            lqw.eq(User::getEmail,iUser.getEmail());
        }
        if (iUser.getStatus() != null){
            lqw.eq(User::getStatus,iUser.getStatus());
        }
        if (iUser.getIsDelete() != null){
            lqw.eq(User::getIsDelete,iUser.getIsDelete());
        }
        if (iUser.getCreateTime() != null){
            lqw.eq(User::getCreateTime,iUser.getCreateTime());
        }
        if (iUser.getUpdateTime() != null){
            lqw.eq(User::getUpdateTime,iUser.getUpdateTime());
        }
        return lqw;
    }
}




